﻿/*
 ***********************************
 时间：2017.9.14
 代码：WangSir
 脚本作用：内推荐模块
 ************************************
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Beyond.Ant;
using Beyond;
using WangSir;
/// <summary>内推荐模块 </summary>
public class InPromotions : MonoBehaviour
{
    private Image mInpromotions_Icon;
    private GameObject mInpromotions_Picture;
    private Image m_Picture;
    private Image m_Left;
    private Image m_Right;
    private Image m_OK;
    private Image m_NO;
    public List<InPromotion> InPromotionList = new List<InPromotion>();
    int Icon_Value;
    int Picture_Value;
    bool getImage;
    public float pictureRate;
    public float iconRate;

    //void OnEnable()
    //{
    //    GameState.isShowStartMenu = false;
    //}

    private void Start()
    {
        mInpromotions_Icon = MyIdolUIFrameWork.Global.FindChild<Image>(transform, "InPromotions_Icon");
        mInpromotions_Picture = MyIdolUIFrameWork.Global.FindChild(transform, "InPromotion_Picture");
        m_Picture = MyIdolUIFrameWork.Global.FindChild<Image>(transform, "Picture");
        m_Left = MyIdolUIFrameWork.Global.FindChild<Image>(transform, "TurnLeft");
        m_Right = MyIdolUIFrameWork.Global.FindChild<Image>(transform, "TurnRight");
        m_OK = MyIdolUIFrameWork.Global.FindChild<Image>(transform, "OK");
        m_NO = MyIdolUIFrameWork.Global.FindChild<Image>(transform, "NO");
        //GetInPromotions();
        LimitPicture();
    }
    private void Update()
    {
        if (!getImage)
        {
            GetInPromotions();
        }
    }

    public void GetInPromotions()
    {
        mInpromotions_Icon.gameObject.SetActive(false);
        mInpromotions_Picture.SetActive(false);

        int inprValue = AppInfo.getSetting("inpr", 0); //接收后台数据
        int tckzValue = AppInfo.getSetting("tckz", 0);
        if (inprValue == 1)
        {
            InPromotionList = InPromotion.getInPromotions(); //接受内推荐信息

            if (InPromotionList.Count != 0)
            {
                //mInpromotions_Icon.gameObject.SetActive(true);
                StartCoroutine(GetImage(InPromotionList[Icon_Value].Icon, mInpromotions_Icon));
                if (tckzValue == 1)
                {
                   // mInpromotions_Picture.gameObject.SetActive(true);
                    StartCoroutine(GetImage(InPromotionList[Picture_Value].Banner, m_Picture));
                }
                getImage = true;

            }
        }
    }

    IEnumerator GetImage(string path, Image picture)
    {
        WWW www = new WWW("file://" + path);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            Texture2D texture2D = www.texture;
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0, 0));
            picture.sprite = sprite;
        }
        
        if(picture=m_Picture)
        {
            mInpromotions_Picture.gameObject.SetActive(true);
        }
        if(picture=mInpromotions_Icon)
        {
            mInpromotions_Icon.gameObject.SetActive(true);
        }
    }


    public void IconToggleEvent()
    {
        if (mInpromotions_Picture.activeSelf == true)
            mInpromotions_Picture.gameObject.SetActive(false);
        else
            mInpromotions_Picture.gameObject.SetActive(true);
    }

    public void BackButtonEvent()
    {
        mInpromotions_Picture.gameObject.SetActive(false);
    }

    public void TurnRight()
    {
        Picture_Value++;
        if (Picture_Value == InPromotionList.Count)
            Picture_Value = 0;
        StartCoroutine(GetImage(InPromotionList[Picture_Value].Banner, m_Picture));
        StartCoroutine(GetImage(InPromotionList[Picture_Value].Icon, mInpromotions_Icon));
    }

    public void TurnLeft()
    {
        Picture_Value--;
        if (Picture_Value < 0)
            Picture_Value = InPromotionList.Count - 1;
        StartCoroutine(GetImage(InPromotionList[Picture_Value].Banner, m_Picture));
        StartCoroutine(GetImage(InPromotionList[Picture_Value].Icon, mInpromotions_Icon));
    }


    public void LimitPicture()
    {
        if (Screen.height < Screen.width)
        {
            m_Picture.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.height * pictureRate, Screen.height * pictureRate);
            m_OK.GetComponent<RectTransform>().localPosition = new Vector2(Screen.height * pictureRate / 2 - 44, Screen.height * pictureRate / 2 - 44);
            m_NO.GetComponent<RectTransform>().localPosition = new Vector2(-Screen.height * pictureRate / 2 + 44, Screen.height * pictureRate / 2 - 44);
            m_Left.GetComponent<RectTransform>().localPosition = new Vector2(-Screen.height * pictureRate / 2 + 35, 0);
            m_Right.GetComponent<RectTransform>().localPosition = new Vector2(Screen.height * pictureRate / 2 - 35, 0);
        }
        else
        {
            m_Picture.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.width * pictureRate, Screen.width * pictureRate);
            m_OK.GetComponent<RectTransform>().localPosition = new Vector2(Screen.width * pictureRate / 2 - 44, Screen.width * pictureRate / 2 - 44);
            m_NO.GetComponent<RectTransform>().localPosition = new Vector2(-Screen.width * pictureRate / 2 + 44, Screen.width * pictureRate / 2 - 44);
            m_Left.GetComponent<RectTransform>().localPosition = new Vector2(-Screen.width * pictureRate / 2 + 35, 0);
            m_Right.GetComponent<RectTransform>().localPosition = new Vector2(Screen.width * pictureRate / 2 - 35, 0);
        }
        mInpromotions_Icon.GetComponent<RectTransform>().localScale = new Vector2(iconRate, iconRate);
    }

    public void PictureEvent()
    {
        InPromotionList[Picture_Value].click();
    }
}
